﻿using System;
using Microsoft.VisualStudio.SharePoint;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;

namespace CodeCounsel.SharePoint.DevTools.ItemTypes
{
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("CodeCounsel.SharePoint.DevTools.FullTrustProxy")]
    public class FullTrustProxy
        : ISharePointProjectItemTypeProvider
    {
        IServiceProvider _serviceProvider;

        public void InitializeType(ISharePointProjectItemTypeDefinition typeDefinition)
        {
            _serviceProvider = typeDefinition.ProjectService.ServiceProvider;
            typeDefinition.Name = "FullTrustProxy";
            typeDefinition.SupportedDeploymentScopes = SupportedDeploymentScopes.Farm;
            typeDefinition.SupportedTrustLevels = SupportedTrustLevels.FullTrust;
            typeDefinition.SupportedAssemblyDeploymentTargets = SupportedAssemblyDeploymentTargets.GlobalAssemblyCache;
            typeDefinition.ProjectItemInitialized += new EventHandler<SharePointProjectItemEventArgs>(typeDefinition_ProjectItemInitialized);
            typeDefinition.ProjectItemAdded += new EventHandler<SharePointProjectItemEventArgs>(TypeDefinition_ProjectItemAdded);
            typeDefinition.ProjectItemPropertiesRequested += new EventHandler<SharePointProjectItemPropertiesRequestedEventArgs>(TypeDefinition_ProjectItemPropertiesRequested);
            FullTrustProxyRefactoring.StartListening(typeDefinition);
        }

        void TypeDefinition_ProjectItemPropertiesRequested(object sender, SharePointProjectItemPropertiesRequestedEventArgs e)
        {
            e.PropertySources.Add(new FullTrustProxyProperties(this, e.ProjectItem));
        }

        void TypeDefinition_ProjectItemAdded(object sender, SharePointProjectItemEventArgs e)
        {
            
        }

        void typeDefinition_ProjectItemInitialized(object sender, SharePointProjectItemEventArgs e)
        {
            IFeatureReceiver receiver = e.ProjectItem.FeatureReceiver;
            if (receiver.Assembly == null && receiver.ClassName == null)
            {
                receiver.Assembly = "$SharePoint.Project.AssemblyFullName$";
                receiver.ClassName = e.ProjectItem.ExtensionData["ReceiverClassName"];
            }
        }
    }
}
